#include "RingBuffer.hpp"
#include <pthread.h>
#include <unistd.h>
#include <ctime>
#include <functional>

using namespace RingBUfferModule;

void *Consumer(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int> *>(args);
    while(true)
    {
        sleep(1);
        int data;
        ring_buffer->Pop(&data);
        std::cout<<"消费了一个数据："<<data<<std::endl;
    }
}

void *Productor(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int> *>(args);
    int data = 0;
    while (true)
    {
        ring_buffer->Equeue(data);
        data++;
        std::cout<<"生成了一个数据："<<data<<std::endl;
    }
}

int main()
{
   RingBuffer<int> *bq = new RingBuffer<int>(5); // 共享资源 -> 临界资源
    // 单生产，单消费
    pthread_t c1, p1; 

    pthread_create(&c1, nullptr, Consumer, bq);
    pthread_create(&p1, nullptr, Productor, bq);
    
    pthread_join(c1, nullptr);
    pthread_join(p1, nullptr);

    delete bq;
    return 0;
}
